﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.IO.Compression;
using Travianer.Model;
using Travianer.Common;
using Travianer.Factory;

namespace Travianer.Controller
{
    public sealed class HandleController
    {
        private static readonly HandleController _Instance;

        static HandleController()
        {
            _Instance = new HandleController();
        }

        public static HandleController GetInstance()
        {
            return _Instance;
        }

        private HandleController()
        {

        }

        public ResponseContainer Login(string strServerLocation, string strUserName, string strPassword, params object[] para)
        {
            string data = string.Format(Configuration.LoginString, strUserName, strPassword);
            return HandlerHelper.GetInstance().IHandler.Post(strServerLocation, data);
        }
        public ResponseContainer Get(string url)
        {
            return HandlerHelper.GetInstance().IHandler.Get(url);
        }
        public ResponseContainer Post(string url, string data)
        {
            return HandlerHelper.GetInstance().IHandler.Post(url, data);
        }

        private ResponseContainer GetEnt(string url)
        {
            var ent = Get(ApplicationHelper.strServerLocation + url);
            if (LogicController.GetInstance().IsLoginForm(ent.Url))
            {
                Login(ApplicationHelper.strServerLocation, ApplicationHelper.strUserName, ApplicationHelper.strPassword);
                ent = Get(ApplicationHelper.strServerLocation + url);
            }
            if (LogicController.GetInstance().IsShownew(ent.Url))
            {
                Get(ApplicationHelper.strServerLocation + "village1.php?_gn_");
                ent = Get(ApplicationHelper.strServerLocation + url);
            }
            return ent;
        }
        private ResponseContainer PostEnt(string url, string data)
        {
            var ent = Post(ApplicationHelper.strServerLocation + url, data);
            if (LogicController.GetInstance().IsLoginForm(ent.Url))
            {
                Login(ApplicationHelper.strServerLocation, ApplicationHelper.strUserName, ApplicationHelper.strPassword);
                ent = Post(ApplicationHelper.strServerLocation + url, data);
            }
            if (LogicController.GetInstance().IsShownew(ent.Url))
            {
                Get(ApplicationHelper.strServerLocation + "village1.php?_gn_");
                ent = Post(ApplicationHelper.strServerLocation + url, data);
            }
            return ent;
        }

        public ResponseContainer GetVillage1(string strVID)
        {
            return GetEnt("village1.php?vid=" + strVID);
        }
        public ResponseContainer GetVillage2(string strVID)
        {
            return GetEnt("village2.php?vid=" + strVID);
        }
        public ResponseContainer GetYZCodeForm(string strVID)
        {
            return GetEnt("plus.php?vid=" + strVID + "&t=2");
        }
        public ResponseContainer DoRes(string strVID, int intResID, string strYZCode)
        {
            return GetEnt("village1.php?vid=" + strVID + "&id=" + intResID + "&k=" + strYZCode);
        }
    }
}
